package com.shujia.wyh.day21;

import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

/*
            TreeMap类概述
                键是红黑树结构，可以保证键的排序和唯一性

 */
public class TreeMapDemo1 {
    public static void main(String[] args) {
        //HashMap<String,String>
        //创建TreeMap集合对象
        TreeMap<String, String> map = new TreeMap<>();

        //向集合中添加元素
        map.put("1001", "张三");
        map.put("1002", "李四");
        map.put("1003", "王五");
        map.put("1001", "赵六");
        map.put("1004", "王二麻");

        //遍历集合
        //方式1：根据键找值
        Set<String> keySet = map.keySet();
        for (String key : keySet) {
            String value = map.get(key);
            System.out.println(key + ":" + value);
        }
        System.out.println("==============================");
        //方式2：根据键值对获取键和值
        Set<Map.Entry<String, String>> entries = map.entrySet();
        for (Map.Entry<String, String> keyValue : entries) {
            String key = keyValue.getKey();
            String value = keyValue.getValue();
            System.out.println(key + "---" + value);
        }
    }
}
